package org.dx.oho.service.impl;

import org.dx.oho.dao.DistrictDao;
import org.dx.oho.entity.District;
import org.dx.oho.service.DistrictService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * create by duanx 2020/11/19
 *@description： AreaService实现类
 *@classname: AreaServiceImpl
 *@author: duanx
 *@date: 2020/11/19 20:47
 **/
@Service
public class DistrictServiceImpl implements DistrictService {

    private final DistrictDao districtDao;

    public DistrictServiceImpl(DistrictDao districtDao) {
        this.districtDao = districtDao;
    }

    /**
     * 所有返回子区划
     * 此方法频繁以父区划ID列为条件查询结果，需要在此列建索引
     * @author: 旭
     * @date: 2020/12/17
     * 当然数据量小就无所谓了
     * @param districts 区划表
     * @param child 子区划表
     */
    private void getChild(@NotNull List<District> districts, List<District> child){
        //存放用来递归的条件
        List<District> districtList = new ArrayList<>();
        for (District value : districts) {
            //构造一个区划，设置父区划ID
            District district = new District();
            district.setParentId(value.getId());
            //查此父ID下的子区划
            List<District> list = districtDao.queryDistrictByRequirement(district);
            //保存到结果里
            child.addAll(list);
            //保存当作下次查询条件
            districtList.addAll(list);
        }
        //更新查询条件
        districts = districtList;
        //如果查询条件集空了就结束,查不到下级子区划了
        if (districts.size()>0){
            getChild(districts,child);
        }
    }


    /**
     * @author: 旭
     * @date: 2020/12/18
     * 返回所有父区划
     * @param district 区划
     * @param parent 父区划集合
     */
    private void getParent(@NotNull District district, List<District> parent) throws Exception {
        District dis = new District();
        if (district.getParentId()==null){
            return;
        }
        dis.setId(district.getParentId());
        List<District> districts = districtDao.queryDistrictByRequirement(dis);
        if (districts.size()>1){
            throw new Exception("父区划有两个-错误");
        }
        if (districts.size()==1){
            parent.addAll(districts);
            getParent(districts.get(0),parent);
        }
    }

    /**
     * 获取给定ID的区划的所有子区划
     * @param id 区划ID
     * @return 给定ID的区划的所有子区划
     */
    @Override
    public List<District> getChildDistrictList(Integer id) throws Exception {
        District district  = new District();
        district.setId(id);
        List<District> districts  = districtDao.queryDistrictByRequirement(district);
        if(districts.size()!=1){
            throw new Exception("未查到区划");
        }
        List<District> childes = new ArrayList<>();
        getChild(districts,childes);
        return childes;
    }


    /**
     * 获取给定ID的区划的所有子区划
     * @param id 区划ID
     * @return 给定ID的区划的所有子区划
     */
    @Override
    public List<District> getParentDistrictList(Integer id) throws Exception {
        District district  = new District();
        district.setId(id);
        List<District> districts  = districtDao.queryDistrictByRequirement(district);
        if (districts.size()!=1){
            throw new Exception("未找到当前区划");
        }
        district = districts.get(0);
        List<District> parents = new ArrayList<>();
        getParent(district,parents);
        return parents;
    }


    @Override
    public List<District> getAllDistrictList() {
        return districtDao.queryDistrictByRequirement(new District());
    }

}
